import { legacy_createStore as createStore, applyMiddleware, compose } from 'redux'
import { persistStore, persistReducer } from 'redux-persist'
import thunkMiddleware from 'redux-thunk'
import logger from 'redux-logger'
import storage from './redux-persist-taro-storage'
import rootReducer from '../reducers'

const persistConfig = {
  key: 'petApp',
  storage,
  throttle: 10, // 解决IOS上无法持久化的问题 https://github.com/NervJS/taro/issues/13551
}

const persistedReducer = persistReducer(persistConfig, rootReducer)

const composeEnhancers = typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
  ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
    // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
  })
  : compose

const middlewares = [
  thunkMiddleware
]

if (process.env.NODE_ENV === 'development') {
  middlewares.push(logger)
}

const enhancer = composeEnhancers(
  applyMiddleware(...middlewares),
  // other store enhancers if any
)

export default function configStore () {
  // const store = createStore(rootReducer, enhancer)
  // return store
  let store = createStore(persistedReducer, enhancer)
  let persistor = persistStore(store)
  return { store, persistor }
}
